package face.basestruct;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.UUID;

/**
 * Created by zhangyanfei on 2019/6/14.
 *
 * @author zhangyanfei
 * @date 2019/06/14
 */
public class SetDemo {
    public static void main(String[] args) {
        Set<Integer> treeSet = new TreeSet<>(Comparator.reverseOrder());

        treeSet.add(5);
        treeSet.add(6);
        treeSet.add(7);
        treeSet.add(8);
        treeSet.add(9);

        for (Integer demo : treeSet) {
            System.out.println(demo);
        }
        // 给key设置一个比较器
        Map<Integer, String> demoMap = new TreeMap<>(Comparator.reverseOrder());

        demoMap.put(1, "aa");
        demoMap.put(2, "bb");
        demoMap.put(3, "cc");

        for (String value : demoMap.values()) {
            System.out.println(value);
        }

        //List<String> unsafeList = new ArrayList<>();
        //
        //for (int i = 0; i < 10; i++) {
        //    new Thread(() -> {
        //        unsafeList.add(UUID.randomUUID().toString().substring(0, 8));
        //        System.out.println(unsafeList);
        //    }, "Thread A").start();
        //}

        Map<Integer, String> unsafeMap = new HashMap<>();
        // 竞争资源（写，读）
        for (int i = 0; i < 10; i++) {
            int finalI = i;
            new Thread(() -> {
                unsafeMap.put(finalI, UUID.randomUUID().toString().substring(0, 8));
                System.out.println(unsafeMap);
            }, "Thread" + i).start();
        }
    }
}
